package dy.spec.edu.week13_home02;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

public class MessageAdapter extends ArrayAdapter<Message> {
    private int itemLayoutId;
    private LayoutInflater mInflator;

    /**
     *
     * @param context
     * @param textViewResourceId    每一项的布局
     * @param objects                数据
     */
    public MessageAdapter(Context context, int textViewResourceId, @NonNull List<Message> objects) {
        super(context, textViewResourceId, objects);
        itemLayoutId = textViewResourceId;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        // 取当前操作的数据项, position表示objects（list）里面的某一个位置
        Message f = getItem(position);
        // 根据模板构造空的View，这个时候还没有图片和文字，只有空的框架
        View view = LayoutInflater.from(getContext()).inflate(itemLayoutId, parent, false);
        NetImageView iv = view.findViewById(R.id.image);
        TextView name1 = (TextView)view.findViewById(R.id.title);
        TextView name2 = (TextView)view.findViewById(R.id.content);
        TextView name3 = (TextView)view.findViewById(R.id.date);
        // 为View的具体内容设置相应的值，ImageView设置图片id，TextView设置文本
        iv.setImageURL("http://www.yyjsx.xyz" + f.getPreview());
        name1.setText(f.getTitle());
        name2.setText(f.getSummary());
        name3.setText(f.getCreateDate());
        return view;
    }

}